{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banner_Top_06.06.18.jpg?raw=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": true
   },
   "source": [
    "<img src=\"https://www.jonar.com/wp-content/uploads/2017/10/robot.jpg\" width=\"500px\" />\n",
    "\n",
    "# Electrical Conductivity\n",
    "\n",
    "Based on LibreTexts library experiment titled \"Electical Conductivity of Aqueous Solutions\" published under the [Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States](https://creativecommons.org/licenses/by-nc-sa/3.0/us/) license"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Background \n",
    "\n",
    "\n",
    "Electrical conductivity is based on the flow of electrons. Metals are good conductors of electricity because they allow electrons to flow through the entire piece of material. Thus, electrons flow like a “sea of electrons” through metals. In comparison, distilled water is a very poor conductor of electricity since very little electricity flows through water. Dissolving ions in these solutions changes this and allows electrons to travel through the solution. If the solution is able to conduct electricity, the current will be captured by a conductivity meter."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Question  \n",
    "How does the conductivity of different solutions compare?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Hypothesis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Enter your hypothesis by double clicking this text."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Materials & Equipment\n",
    "\n",
    "* conductivity meter\n",
    "* wash bottle with distilled water\n",
    "* large beaker for rinsing/waste\n",
    "* sodium chloride\n",
    "* sugar\n",
    "* tissues \n",
    "\n",
    "Solutions: \n",
    "* acetic acid (vinegar) \n",
    "* tap water \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Procedure\n",
    "1. Use a wash bottle with distilled water and a large beaker labeled “waste” to rinse the electrodes. Dry using a  tissue. When switched on, the meter should report 0 (or very close to it).\n",
    "2. Put 50 mL of distilled water into a beaker.  \n",
    "3. Place the metal tips of your conductivity tester in the solution.\n",
    "4. Record the conductivity reading of the distilled water in the table below.  \n",
    "\n",
    "5. Repeat steps 1-4 with samples of the other solutions. For example, take a spoonful of salt and dissolve it in water. After each conductivity measurement, empty the beaker and rinse it with distilled water. \n",
    "\n",
    "6. Clean up when done."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<script>\n",
    "  function code_toggle() {\n",
    "    if (code_shown){\n",
    "      $('div.input').hide('500');\n",
    "      $('#toggleButton').val('Show Code')\n",
    "    } else {\n",
    "      $('div.input').show('500');\n",
    "      $('#toggleButton').val('Hide Code')\n",
    "    }\n",
    "    code_shown = !code_shown\n",
    "  }\n",
    "  \n",
    "  $( document ).ready(function(){\n",
    "    code_shown=false;\n",
    "    $('div.input').hide()\n",
    "  });\n",
    "</script>\n",
    "<form action=\"javascript:code_toggle()\"><input type=\"submit\" id=\"toggleButton\" value=\"Show Code\"></form>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import qgrid\n",
    "df = pd.DataFrame(index=pd.Series(['Distilled Water', 'Tap Water', 'Salt Water', 'Vinegar']), columns=pd.Series(['Reading 1', 'Reading 2','Notes']))\n",
    "df_widget = qgrid.QgridWidget(df =df, show_toolbar=True)\n",
    "df_widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Javascript, display\n",
    "from ipywidgets import widgets\n",
    "\n",
    "def run_all(ev):\n",
    "    display(Javascript('IPython.notebook.execute_cell_range(IPython.notebook.get_selected_index()+1,IPython.notebook.get_selected_index()+2)'))\n",
    "\n",
    "button = widgets.Button(description=\"Click to save data\")\n",
    "button.on_click(run_all)\n",
    "display(button)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_data = df_widget.get_changed_df()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Questions\n",
    "\n",
    "1. Why must the electrodes on the conductivity apparatus, as well as all the beakers, be rinsed with distilled water after each conductivity test?\n",
    "2. Which solution conducted electricity the best? How can you tell? \n",
    "3. Why did one solution conduct electricity better than another? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Answers\n",
    "\n",
    "1.  \n",
    "2.  \n",
    "3.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conductivity in your neighborhood\n",
    "Electrical conductivity can be an important indicator of what is present in a given solution. Conductivity increases as more ions are dissolved in water. Therefore, the conductivity level can be used as an indicator for the water quality. For example, water treatment plants that process sea water and turn it into usable water use conductivity as an indicator for how well the plant removes salt from the water. In this case, a certain maximum conductivity level is set as a cutoff value for how much salt is allowed in the water before it can be used in a home or a mining operation. \n",
    "\n",
    "The table below shows you readings from the Rossdale Epcor water treatment plant in Edmonton over the last week. This plant supplies downtown Edmonton and the ELS region, covering the Northwest, West, and Southwest. Note that one of the quality indicators reported below is conductivity!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "url_ELS = \"http://apps.epcor.ca/DAilyWaterQuality/Default.aspx?zone=ELS\"\n",
    "url_RD = \"http://apps.epcor.ca/DAilyWaterQuality/Default.aspx?zone=Rossdale\"\n",
    "table_RD = pd.read_html(url_RD, header=0)\n",
    "table_ELS = pd.read_html(url_ELS, header=0)\n",
    "qgrid.QgridWidget(df = table_RD[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We've taken the data from the water treatment plants along with the readings from your experiment and graphed the conductivity in the graph below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "button = widgets.Button(description=\"Update the plot\")\n",
    "button.on_click(run_all)\n",
    "display(button)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.colors import Normalize\n",
    "import matplotlib.cm as cm\n",
    "import warnings\n",
    "# ignoring runtime warning from taking the mean of a single number\n",
    "# this also catches strings. \n",
    "np.warnings.filterwarnings('ignore')\n",
    "cmap = cm.Set1\n",
    "\n",
    "norm=Normalize(vmin=0, vmax=len(output_data.index)+1)\n",
    "\n",
    "for i in range(len(output_data.index)): \n",
    "    try:\n",
    "        sample_mean=np.nanmean([float(output_data.iloc[i,0]),float(output_data.iloc[i,1])])\n",
    "        plt.axhline(y=sample_mean, c=cmap(norm(i)), ls='dashed', label='Average of ' + output_data.index[i], lw='3')\n",
    "\n",
    "    except ValueError as e:\n",
    "        # skip data that has strings in it. \n",
    "        continue\n",
    "\n",
    "\n",
    "plt.plot(table_RD[0].iloc[6, 1:8], 'bo-', label='Rossdale readings')\n",
    "#plt.plot(pd.DataFrame(table_RD[0].iloc[6, 1:8].apply(pd.to_numeric)))\n",
    "plt.xlabel('Day of Measurement')\n",
    "plt.ylabel('Conductivity (yS/cm)')\n",
    "plt.title('Conductivity in treated waste water over the last week')\n",
    "plt.legend(bbox_to_anchor=(1.05, 1), loc=2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "4.\n",
    "How do your values compare? What does this say about the water leaving the Rossdale water treatment plant? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Answer\n",
    "4. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
